#-*- coding:utf-8 -*-

from datetime import datetime

class EntBase:

    "获取doc字典中某个key的值"
    def getVal(self,doc,key,default=''):
        if doc and doc.has_key(key):
            return doc[key]
        else:
            return default


    "获取保存到mongo的document"
    def getDoc(self,idName = 'id'):
        doc = self.__dict__
        if doc[idName]:
            doc['_id'] = ObjectId(idName)
        del doc[idName]
        return doc

class User(EntBase):

    #stat:0-正常 4-删除
    STAT_NORMAL = 0
    STAT_DEL = -1

    def __init__(self, doc = None):
        self.uid = str(self.getVal(doc,'_id'))
        self.name = self.getVal(doc,'name')
        self.mobile = self.getVal(doc,'mobile')
        self.state = self.getVal(doc,'state',self.STAT_NORMAL)
        self.mtime = self.getVal(doc,'mtime',datetime.now())

    def getDoc(self):
        return EntBase.getDoc(self,'uid')

class Car(EntBase):

    def __init__(self, doc = None):
        self.id = str(self.getVal(doc,'_id'))
        #所属用户id
        self.uid = self.getVal(doc,'uid')
        #名称
        self.name = self.getVal(doc,'name')
        #停车场id
        self.pid = self.getVal(doc,'pid')
        #位置
        self.loc = self.getVal(doc,'loc')
        #车图片id
        self.pic = self.getVal(doc,'pic')
        self.mtime = self.getVal(doc,'mtime',datetime.now())

class Park(EntBase):

    def __init__(self, doc = None):
        self.pid = str(self.getVal(doc,'_id'))
        self.name = self.getVal(doc,'name')
        self.loc = self.getVal(doc,'loc',None)
        self.ownerId = self.getVal(doc,'ownerId')
        self.spaceTotal = self.getVal(doc,'spaceTotal',0)
        self.spaceLeft = self.getVal(doc,'spaceLeft',0)
        self.price = self.getVal(doc,'price',0.0)
        self.pics = [] #图片是多张，所以是数组
        self.mtime = self.getVal(doc,'mtime',datetime.now())

class Pic(EntBase):

    def __init__(self, doc = None):        
        self.pid = str(self.getVal(doc,'_id'))
        self.fileName = self.getVal(doc,'fileName')

class Comment(EntBase):
    #评论状态,0:普通 -1:删除
    STAT_NORMAL = 0
    STAT_DEL = -1

    def __init__(self, doc = None):
    	self.cid = str(self.getVal(doc,'_id'))
    	self.parkId = self.getVal(doc,'parkId')
    	self.uid = self.getVal(doc,'uid')
    	self.content = self.getVal(doc,'content') 
        self.stat = self.getVal(doc,'stat',self.STAT_NORMAL)
